package jalus.blog;

import jalus.Action;
import jalus.pojo.Post;
import jalus.pojo.ReaderFeed;
import jalus.service.ConfigureService;
import jalus.service.PostService;
import jalus.service.ReaderService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class ReaderServlet extends Action {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String feed = req.getParameter("feed");
		if(StringUtils.isNotEmpty(feed)){
			singleFeed(req, resp, feed);
		}else{
			feed(req, resp);
		}

	}
	
	private void feed(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		String uri = req.getRequestURI();
		String flag = req.getParameter("flag");
		
		ReaderService readerService = ReaderService.getInstance();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("focustab", "reader");
		root.put("config", ConfigureService.getInstance());
		root.put("spinpostlist", PostService.getInstance().getSpinPostList());
		root.put("userService", userService);
		root.put("uri", uri);
		
		//Template template;
		List<jalus.pojo.Feed> feedList;
		List<Post> postList;
		String nextFlag;
		try {
			readerService.login();
			feedList = readerService.getFeedList();
			ReaderFeed readerFeed  = readerService.getSharedReadingList(flag);
			postList = readerFeed.getPostList();
			nextFlag = readerFeed.getFlag();
		} catch (Exception e) {
			throw new IOException();
		} 
		
		root.put("feedlist", feedList);
		root.put("entrylist", postList);
		if(StringUtils.isNotEmpty(flag)){
			root.put("prevflag", "/reader?flag=" + flag);
		}
		if(StringUtils.isNotEmpty(nextFlag)){
			root.put("nextflag", "/reader?flag=" + nextFlag);
		}
		
		processTemplate(root, "reader.html", resp);
	}

	private void singleFeed(HttpServletRequest req, HttpServletResponse resp, String feed) throws IOException{
		UserService userService = UserServiceFactory.getUserService();
		String uri = req.getRequestURI();
		String flag = req.getParameter("flag");
		
		ReaderService readerService = ReaderService.getInstance();
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("focustab", "reader");
		root.put("config", ConfigureService.getInstance());
		root.put("spinpostlist", PostService.getInstance().getSpinPostList());
		root.put("userService", userService);
		root.put("uri", uri);
		
		List<Post> postList;
		String nextFlag;
		readerService.login();
		try {
			ReaderFeed readerFeed = readerService.getFeedByUri(feed, flag);
			postList = readerFeed.getPostList();
			nextFlag = readerFeed.getFlag();
		} catch (Exception e) {
			error();
			return;
		} 
		
		root.put("entrylist", postList);
		root.put("feedlist", readerService.getFeedList());
		if(StringUtils.isNotEmpty(flag)){
			root.put("prevflag", "/reader?feed=" + feed + "&flag=" + flag);
		}
		if(StringUtils.isNotEmpty(nextFlag)){
			root.put("nextflag", "/reader?feed=" + feed + "&flag=" + nextFlag);
		}
		
		processTemplate(root, "reader.html", resp);
	}
	
}
